package com.borqs.samples;

import android.util.*;
import android.content.*;
import java.util.*;

public class AlarmSampler extends ASampler{
    private final static String TAG = "AlarmSampler";

    public AlarmSampler(DBHelper helper){
        super(helper);
        AlarmServiceProvider.enableStats(true);//enable stats
    }
    @Override public void sample(long sample_id){
        Map<String,Object> mystats = AlarmServiceProvider.getStatsInfo();
        boolean enabled = (Boolean) mystats.get("enabled");
        long startTime = (Long) mystats.get("startTime");
        long stopTime = (Long) mystats.get("stopTime");
        //every pkg's wakeup count
        Map<String,Integer> pkgCount = (Map<String,Integer>) mystats.get("pkgCount");
        Log.d(TAG,"enabled="+enabled+","+startTime+"->"+stopTime+"==>"+pkgCount);
        dbSave(sample_id,pkgCount);
    }
    private void dbSave(long sample_id,Map<String,Integer> pkgCount){
        ContentValues cv = new ContentValues();
        for(Map.Entry<String,Integer> entry : pkgCount.entrySet()){
            cv.put("pkg",entry.getKey());
            cv.put("count",entry.getValue());
            cv.put("sample_id",sample_id);
            helper.getWritableDatabase().insert("alarm_samples",null,cv);
        }
    }
    /*alarm things*/
    private void testGetActionInfo(){
        AlarmServiceProvider.enableAction(true);
        Map<String,Object> info = AlarmServiceProvider.getActionInfo();
        boolean enabled = (Boolean) info.get("enabled");
        long startTime = (Long) info.get("startTime");
        long stopTime = (Long) info.get("stopTime");
        Map<String,String[]> pkgAction = (Map<String,String[]>) info.get("pkgAction");
        Log.d(TAG,"enabled="+enabled+","+startTime+"->"+stopTime+"==>"+pkgAction);
        for(Map.Entry<String,String[]> entry : pkgAction.entrySet()){
            String[] values = entry.getValue();
            Log.d(TAG,entry.getKey()+"--"+values[0]+","+values[1]);
        }
    }
    private void testAddActionRule(){
        String pkg = "com.borqs.tests";
        AlarmServiceProvider.removeActionRule(pkg);
        List<String> params = new ArrayList<String>();
        params.add("5000");
        AlarmServiceProvider.addActionRule(pkg,"DelayAction",params);
        AlarmServiceProvider.addActionRule(pkg,"MergeAction",params);
        /*
        AlarmServiceProvider.addActionRule("com.borqs.tests","MergeAction",params);
        AlarmServiceProvider.addActionRule("com.borqs.tests","CountAction",params);
        AlarmServiceProvider.addActionRule("com.borqs.tests","DelayAction",params);
        */


        //testGetActionInfo();
    }
    private void testGetStatsInfo(){
        //
        AlarmServiceProvider.enableStats(true);
        Map<String,Object> mystats = AlarmServiceProvider.getStatsInfo();
        boolean enabled = (Boolean) mystats.get("enabled");
        long startTime = (Long) mystats.get("startTime");
        long stopTime = (Long) mystats.get("stopTime");
        Map<String,Integer> pkgCount = (Map<String,Integer>) mystats.get("pkgCount");
        Log.d(TAG,"enabled="+enabled+","+startTime+"->"+stopTime+"==>"+pkgCount);
    }
}
